﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using OpenTKLightingTool.Forms;
using OpenTKLightingTool.Data;
using Ignitron.EEngine;

namespace OpenTKLightingTool
{
    /// <summary>
    /// The lighting tool facade class
    /// </summary>
    public class LightingTool
    {
        /// <summary>
        /// Reference to the window
        /// </summary>
        private LightingToolWindow mWindow = null;

        /// <summary>
        /// Holder of all data-layer classes
        /// </summary>
        private DataStorage mStorage;

        /// <summary>
        /// Ignitron Engine Wrapper
        /// </summary>
        private IEngine mEngine;


        /// <summary>
        /// Event raised when a re-rendering is requested
        /// </summary>
        public event EventHandler RenderRequestedEvent;

        /// <summary>
        /// True, if the window is shown
        /// </summary>
        public bool IsVisible
        {
            get
            {
                return mWindow != null;
            }
        }

        /// <summary>
        /// Constructor
        /// </summary>
        public LightingTool(IEngine engine)
        {
            mEngine = engine;
            mStorage = new DataStorage(mEngine);
        }
       
        /// <summary>
        /// Shows the settings window
        /// </summary>
        public void Show()
        {
            if (mWindow != null)
                return;
            mWindow = new LightingToolWindow(mEngine);
            mWindow.SetStorage(mStorage);
            mWindow.RenderPressedEvent += mWindow_RenderPressedEvent;
            mWindow.Closed += mWindow_Closed;
            mWindow.Topmost = true;

            // set position
            mWindow.WindowStartupLocation = WindowStartupLocation.CenterScreen;
            //double left = Application.Current.MainWindow.Left + Application.Current.MainWindow.ActualWidth - mWindow.Width;
            //double top = Application.Current.MainWindow.Top;
            //mWindow.Left = left;
            //mWindow.Top = top;

            mWindow.Show();
        }


        /// <summary>
        /// Hides the settings window
        /// </summary>
        public void Hide()
        {
            if (mWindow == null)
                return;
            mWindow.Close();
            mWindow = null;
        }

        /// <summary>
        /// Adds a material with a given name
        /// </summary>
        public void AddMaterialItem(string materialName)
        {
            mStorage.Materials.New(materialName);
        }

        /// <summary>
        /// Renders the lights
        /// </summary>
        public void RenderLighting()
        {
            mStorage.Lighting.Render(mEngine);
            mStorage.Lights.Render(mEngine);
            mStorage.AxesSystem.Render(mEngine);
            if(mStorage.AxesSystem.ShowLights)
            {
                mStorage.Lights.RenderLightCrosses(mEngine);
            }
        }

        /// <summary>
        /// Renders a given material specified by its name
        /// </summary>
        public void RenderMaterial(string materialName)
        {
            mStorage.Materials.Render(materialName, mEngine);
        }

        /// <summary>
        /// Invoked when the window is closed
        /// </summary>
        private void mWindow_Closed(object sender, EventArgs e)
        {
            mWindow.Closed -= mWindow_Closed;
            mWindow.RenderPressedEvent -= mWindow_RenderPressedEvent;
            mWindow = null;
        }

        /// <summary>
        /// Invoked when Render button is pressed
        /// </summary>
        private void mWindow_RenderPressedEvent(object sender, EventArgs e)
        {
            if (RenderRequestedEvent != null)
            {
                RenderRequestedEvent(this, EventArgs.Empty);
            }
        }

    }
}
